﻿using System.Collections.Generic;

namespace Utilities.Validation
{
    public class DataValidate
    {
        /// <summary>
        ///     初始化
        /// </summary>
        public DataValidate()
        {
            Opts = new Dictionary<string, object>();
        }

        /// <summary>
        ///     规则存储键值对应:({{"rules": {"email": {"required": true,"email": true} } },{"messages": {"email": {"required":"不输入email你怎么登陆?","email": "格式错误"} } }})
        /// </summary>
        public Dictionary<string, object> Opts { get; set; }

        /// <summary>
        ///     要验证的名称
        /// </summary>
        public string Name { get; set; }

        ///// <summary>
        /////     错误提示信息，没有则会按照默认提示
        ///// </summary>
        //public string Messages { get; set; }

        /// <summary>
        ///     设置提示信息,设置设置js验证时自动调用
        /// </summary>
        private void SetMessage(string validateName,string strMessage)
        {
            if (strMessage == null) return;
            //提示信息
            var message = new Dictionary<string, string> { { validateName, strMessage } };
            if (Opts.ContainsKey("messages"))//有messages验证
            {
                if (!(Opts["messages"] as Dictionary<string, object>).ContainsKey(Name))//没有有要添加的验证
                {
                    (Opts["messages"] as Dictionary<string, object>).Add(Name, message);
                }
                else
                {
                    if (!((Opts["messages"] as Dictionary<string, object>)[Name] as Dictionary<string, string>).ContainsKey(validateName))//没有要添加的验证
                    {
                        ((Opts["messages"] as Dictionary<string, object>)[Name] as Dictionary<string, string>).Add(validateName, strMessage);
                    }
                    //(Opts["messages"] as Dictionary<string, object>)[Name]
                }
            }
            else
            {
               
                var messages = new Dictionary<string, object>();
                messages[Name] = message;
                Opts["messages"] = messages;
            }
        }

        /// <summary>
        ///     设置js验证，
        /// </summary>
        /// <param name="valType">类型</param>
        /// <param name="value">参数</param>
        private void SetRules(string valType, object value)
        {
            if (Opts.ContainsKey("rules"))//---------------------------------存在rules键
            {
                if ((Opts["rules"]as Dictionary<string, object>).ContainsKey(Name))
                {
                    if (!((Dictionary<string, object>)((Opts["rules"] as Dictionary<string, object>)[Name])).ContainsKey(valType))
                    {
                        ((Dictionary<string, object>)((Opts["rules"] as Dictionary<string, object>)[Name])).Add(valType, value);
                    }
                    
                }
                else
                {
                    var rulesItem = new Dictionary<string, object>();
                    rulesItem[valType] = value;

                    (Opts["rules"] as Dictionary<string, object>)[Name] = rulesItem;
                }
            }
            else
            {
                var rulesItem = new Dictionary<string, object>();//--------------------{"required": true,"email": true}
                rulesItem[valType] = value;

                var rules = new Dictionary<string, object>();//------------------------{"email": {"required": true,"email": true} }
                rules[Name] = rulesItem;

                Opts["rules"] = rules;//---------------------------------------------{"rules": {"email": {"required": true,"email": true} } }
            }
        }

        public void SetOpts(string valType, object value,string message)
        {
            SetRules(valType, value);
            if (message!=null)
            {
                SetMessage(valType, message);
            }            
        }
        /// <summary>
        /// 添加最外层
        /// </summary>
        /// <param name="valType"></param>
        /// <param name="value"></param>
        public void SetOpts1(string valType, object value)
        {
            Opts[valType] = value;
        }
    }
}